<?php
class WebUser extends CWebUser
{
    // Store model to not repeat query.
    private $_model;
    
    public function __get($name)
    {
        if ($this->hasState('__userInfo')) {
            $user=$this->getState('__userInfo',array());
            if (isset($user[$name])) {
                return $user[$name];
            }
        }
 
        return parent::__get($name);
    }
 
    public function login($identity, $duration) {
        $this->setState('__userInfo', $identity->getUser());
        parent::login($identity, $duration);
    }

    public function getModel($id=null)
    {
        if($this->_model===null)
        {
            if($id!==null) {
                $this->_model=User::model()->findByPk($id);
            } else {
                if(!Yii::app()->user->isGuest)
                    $this->_model = User::model()->findByPk(Yii::app()->user->id);
            }
        }
        return $this->_model;
    }

}
